#ifndef NETWORKDAV_H
#define NETWORKDAV_H
//
#include "main.h"
#include "base_modul.h"
#include "qxml.h"
#include "basedav.h"
#include <QObject>
#define DAVM1_ "OPTIONS"
#define DAVM2_ "PROPFIND"
#define DAVM3_ "PROPPATCH"
#define DAVM4_ "PUT"
#define DAVM5_ "DELETE"
#define DAVM6_ "MKCOL"

/* container from user pas url e root path operator */


typedef QMap<int, QStringList> LogConnection;

#define INFODIRXMLFORMAT   0
#define INFODIRLISTFORMAT   1

    

class NetworkDav: public Base_Modul
{

    
public:	 
    NetworkDav( QString w , QString u , QString p , int portopen )
{
    QUrl urlg(w,QUrl::StrictMode);
    host = urlg.host();
    basepath = urlg.path();
    url = w;
    user = u;
    pass = p;
    port = portopen;
    dummypath = CheckSlash(urlg.path());
}

QString FileResolverPut(  QFileInfo  upload  , const QString path  )
{
    QString netname = Resolvpath(path);
    return  netname.append(FileNameFix(upload.fileName()));
}



void Append_Url( const QString remoteurl , const QString localfile , const QString method )
{
    UrlOpen.insert(UrlOpen.size(),QStringList() << remoteurl << localfile << method);
}
void Register_Path( const QString remotepath , const QString localfile , const QString method )
{
    QString fromtime = QString("%1").arg(QTime_Null());
    PathRegister.insert(InsertIndex(remotepath),QStringList() << remotepath << localfile << method << fromtime);
}

bool HavingPath( QString path ) 
{
    bool Ihave = false;
      LogConnection::Iterator it;
                    for ( it = PathRegister.begin(); it != PathRegister.end(); ++it ) { 
                        QStringList itemsetter = it.value(); 
                        QString opath = itemsetter.at(0);
                        QString ofile = itemsetter.at(1);
                        int timerget = direct_int(itemsetter.at(3));
                        if (path == opath and is_file(ofile)) {
                           /////////////////qDebug() << "####" << itemsetter.at(0) << "######" << itemsetter.at(1) << "#####" <<  itemsetter.at(3) << "#####"; 
                            return true;
                        }
                    }
   return Ihave;
}

int InsertIndex( QString path ) 
{
      int insertatqw = PathRegister.size();
      LogConnection::Iterator it;
                    for ( it = PathRegister.begin(); it != PathRegister.end(); ++it ) { 
                        QStringList itemsetter = it.value(); 
                        int posi = it.key();
                        QString opath = itemsetter.at(0);
                        QString ofile = itemsetter.at(1);
                        int timerget = direct_int(itemsetter.at(3));
                        if (path == opath and is_file(ofile)) {
                          return posi;
                        }
                    }
       return insertatqw;
}






QString GetStreamPath( QString path ) 
{
QString xml;
      LogConnection::Iterator it;
                    for ( it = PathRegister.begin(); it != PathRegister.end(); ++it ) { 
                        QStringList itemsetter = it.value(); 
                        QString opath = itemsetter.at(0);
                        QString ofile = itemsetter.at(1);
                        int timerget = direct_int(itemsetter.at(3));
                        if (path == opath and is_file(ofile)) {
                           return fopenutf8(ofile);
                        }
                    }
return xml;
}


  QString ResolvDisplayPath( QString path ) 
{
    if (path == "/" ) {
    return path;
    } else {
    return CheckSlash(path.replace(dummypath,"/"));
    }
}
  QString Resolvpath( QString pathopen ) 
{
    ///////int strsize = pathopen.size();
    QString full,makepath;
    QString openmydir = "/";
    bool isroot = false;
    bool isssl = false;
    if (url.startsWith("https://")) {
       isssl = true;
     }
    
    
    QUrl wurl( url , QUrl::StrictMode );
    dummypath = CheckSlash(wurl.path());
     
    if (pathopen == "/" ) {
       return dummypath;
    } else {
      openmydir = CheckSlash(pathopen); 
    }
    
    if (isroot) {
    full = url;
    DisplayName = "/";
    } else {
    DisplayName = openmydir;  
        
    makepath = CheckSlash(QString("%1%2").arg(dummypath).arg(openmydir));
        
        if (isssl) {
        full = QString( "https://%1%2" ).arg(wurl.host()).arg( makepath );   /* url encode ! */
        } else {
        full = QString( "http://%1%2" ).arg(wurl.host()).arg( makepath );   /* url encode ! */
        }            
    }
    QUrl wurl2( full , QUrl::StrictMode );
    
   //////////////// qDebug() << "####### full -> " << full; 
    //////////qDebug() << "####### DisplayName -> " << DisplayName; 
    //////////qDebug() << "####### dummypath -> " << dummypath; 
    
    return wurl2.path();
}
inline QString Host() const { return host; }
inline QString Basepath() const { return basepath; }
inline QString Url() const { return url; }
inline QString User() const { return user; }
inline QString Pass() const { return pass; }
inline QString DummiRoot() const { return dummypath; }
inline int Port() const { return port; }
protected:	 
    QString url;
    QString basepath;
    QString user;
    QString pass;
    QString host;
    QString DisplayName;
    QString dummypath;
    int port;
//
private:
    LogConnection UrlOpen;
    LogConnection PathRegister;



////signals:
/////public slots:

};







//
/*  Save file as networkdav.h  */
/*  incomming class name NetworkDav */
//



//
#endif // NETWORKDAV_H

